#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
    echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    echo "macOS system."
    if [ -f ~/.bash_profile ]; then
        source ~/.bash_profile
    fi
elif current_os_is_windows; then
    echo "Windows system."
else
    echo "Unknown OS"
fi

APP_INSTALL_NAME="index-tts2"
APP_TARGET_DIR_NAME="index-tts2"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/download_utils.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/print_sys_info.sh"
# source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"

mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: $CURRENT_APP_PATH"
else
    echo "Error, App Dir dose not exist, please retry. $CURRENT_APP_PATH"
    exit 1
fi

echo "lmdprogress:12"

export GIT_LFS_SKIP_SMUDGE=1
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
function clone_app_repo_via_gitee {
  export GIT_HOST="https://gitee.com"
  export GIT_REPO_URL="${GIT_HOST}/lmdown/index-tts-2"
  export GIT_REPO_BRANCH="master"
  sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"
}

export GITHUB_GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GITHUB_GIT_REPO_URL="${GITHUB_GIT_HOST}/index-tts/index-tts"

clone_app_repo_via_gitee

echo "lmdprogress:30"

cd "$CURRENT_APP_PATH"

echo "lmdprogress:35"

CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi
# INDEX_TTS_ENV_NAME=$APP_INSTALL_NAME
export PYTHON_VER_ENV=3.10

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    export PYTHON_VER_ENV=3.12
    conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y
elif current_os_is_windows; then
    conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y
fi



echo "lmdprogress:45"

install_torch() {
    if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
        # conda_run_pip_install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
        conda_run_conda_forge_install ffmpeg
    elif current_os_is_windows; then
        export INSTALL_TORCH_VERSION="2.8.0"
        export INSTALL_TORCH_INDEX_PATH="cu128"
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
        source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/check_ffmpeg_and_install_win.sh"
    fi
}
install_torch

echo "lmdprogress:50"

conda_run_pip_install py-cpuinfo
echo "lmdprogress:51"
if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    echo "macOS system."
    conda_run_pip_install wetext 
    conda_run_pip_install deepspeed
    PROJ_TOML_FILE="pyproject.toml"
    PROJ_TOML_FILE_BAK="pyproject-bak.toml"
    mv $PROJ_TOML_FILE $PROJ_TOML_FILE_BAK
    grep -vE 'numba==' $PROJ_TOML_FILE_BAK > $PROJ_TOML_FILE
    conda_run_pip_install numba==0.61.2
elif current_os_is_windows; then
    echo "Windows system."
    conda_run_pip_install "$GITHUB_GIT_HOST/daswer123/deepspeed-windows/releases/download/13.1/deepspeed-0.13.1+cu121-cp310-cp310-win_amd64.whl"
    conda_run_conda_forge_install pynini==2.1.6
    
    echo "lmdprogress:60"
    conda_run_pip_install WeTextProcessing==1.0.4.1 einops==0.8.1
    # conda_run_pip_install einops==0.8.1
fi

echo "lmdprogress:66"

comment_deepspeed() {
  proj_file="${CURRENT_APP_PATH}/pyproject.toml"
  sed -i '/^[^#].*deepspeed==/s/^/#/' "$proj_file"
  echo "add comment sign for deepspeed=="
}

comment_deepspeed

conda_run_pip_install -e .

echo "lmdprogress:67"
conda_run_pip_install huggingface_hub==0.34.2 hf_xet filelock fsspec httpx==0.28.1 py-cpuinfo


echo "lmdprogress:70"
conda_run_pip_install -e ".[webui]"

echo "lmdprogress:75"

# download models
if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "lmdprogress:80"
conda_run_pip_install modelscope==1.31.0
echo "lmdprogress:81"
LOCAL_MODEL_DIR="checkpoints"

export HF_HOME="$CURRENT_APP_PATH/tmp_hf_home"
export MODELSCOPE_HOME="$CURRENT_APP_PATH/tmp_modelscope_home"
export MODELSCOPE_CACHE="$CURRENT_APP_PATH/tmp_modelscope_cache"

if [ -n "$HF_ENDPOINT" ]; then
    conda_run_cmd modelscope download --model IndexTeam/IndexTTS-2 --local_dir $LOCAL_MODEL_DIR
else
    conda_run_hf_download IndexTeam/IndexTTS-2 --local-dir $LOCAL_MODEL_DIR
fi


if [ -n "$HF_ENDPOINT" ]; then
    # install with modelscope and save as hf dir
    export HF_HOME="$CURRENT_APP_PATH/tmp_hf_home_by_modelscope"
    export TRANSFORMERS_CACHE="./tmp_hf_home_by_modelscope/hub"
    export TRANSFORMERS_OFFLINE=1
    export HF_HUB_DISABLE_SYMLINKS=1
    echo "lmdprogress:82"
    conda_run_hf_dl_by_modelsople "facebook/w2v-bert-2.0" facebook--w2v-bert-2.0
    echo "lmdprogress:84"
    conda_run_hf_dl_by_modelsople "amphion/MaskGCT semantic_codec/model.safetensors" amphion--MaskGCT
    echo "lmdprogress:86"
    conda_run_hf_dl_by_modelsople "iic/speech_campplus_sv_zh-cn_16k-common campplus_cn_common.bin" funasr--campplus
    echo "lmdprogress:90"
    conda_run_hf_dl_by_modelsople "nv-community/bigvgan_v2_22khz_80band_256x config.json bigvgan_generator.pt" nvidia--bigvgan_v2_22khz_80band_256x
else
    echo "lmdprogress:82"
    conda_run_hf_download facebook/w2v-bert-2.0
    echo "lmdprogress:84"
    conda_run_hf_download amphion/MaskGCT semantic_codec/model.safetensors
    echo "lmdprogress:86"
    conda_run_hf_download funasr/campplus campplus_cn_common.bin
    echo "lmdprogress:90"
    conda_run_hf_download nvidia/bigvgan_v2_22khz_80band_256x config.json bigvgan_generator.pt
fi

echo "lmdprogress:100"


